package com.rest.demo.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.alibaba.fastjson.JSONObject;


public class PYEncryption {
	private static final String password = "ANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6Z1NnOlQfHfTFLVZeODCyOU9EAz8vORrB68";
	private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding";//默认的加密算法
    /**
     * AES 加密操作
     * @param content 待加密内容
     * @parampassword2 加密密钥
     * @return 返回Base64转码后的加密数据
     */
    public static String encrypt(String content) {
        try {
	        SecretKeySpec skeySpec = getKey();
	        byte[] clearText = content.getBytes("UTF8");
	        MessageDigest md5 = MessageDigest.getInstance("MD5"); 
	        byte[] iv = md5.digest(password.getBytes("utf-8")); 
	        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
	        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
	        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
	        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
	        
	        String encrypedValue =Encodes.encodeBase64(cipher.doFinal(clearText));
	        return encrypedValue;
        } catch (Exception ex) {
        }
        return null;
    }
 
    /**
     * AES 解密操作
     *
     * @param content 待解密内容
     * @parampassword 解密密钥
     * @return 返回解密后数据
     */
    public static String decrypt(String content) {
        try {
	        SecretKey key = getKey();
	        MessageDigest md5 = MessageDigest.getInstance("MD5"); 
	        byte[] iv = md5.digest(password.getBytes("utf-8")); 
	        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
	        
	        byte[] encrypedPwdBytes = Encodes.decodeBase64(content);
	        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
	        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
	        cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
	        byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
	        
	        String decrypedValue = new String(decrypedValueBytes , "UTF-8");
	        return decrypedValue;
        } catch (Exception ex) {
        	//System.err.println("Decrypt exception: " + ex.getMessage());
        }
        return null;
    }
    
	private static SecretKeySpec getKey() throws NoSuchAlgorithmException, UnsupportedEncodingException {
	    MessageDigest messageDigest  = MessageDigest.getInstance("SHA-256");
	    byte[] passwordBytes = password.getBytes("UTF-8");
	    byte[] keyBytes = messageDigest.digest(passwordBytes); 
	    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
	    return key;
	}


	public static void main(String[] args) {
		JSONObject o = new JSONObject();
		o.put("Appid", "FIXF789654123");
		o.put("AppSecret", "3e00eaede36c453aa4f70ad4517bc95f");
		System.out.println(decrypt("loZ7+pIweIjceR7otyoBHBIxJY/p0gkkqRGJMPzgtz0h2oFXCFrM8ZEH1MTobiYr6JuKCWmESEwAkvqxJoTHkq9CP7w5qXn4ADfpLxZIe8I="));
	}

}
